﻿Imports Microsoft.VisualBasic

Namespace My
    Public Class MyDropDownList
        Inherits DropDownList

        Protected Overrides Sub OnDataBound(ByVal e As System.EventArgs)
            If Me.Items.FindByValue("") Is Nothing And (Me.CancelAppendNullOnSingleItem = False Or Me.Items.Count <> 1) Then
                If Me.AppendPleaseChooseItem = True Then
                    Me.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
                End If

                If Me.AppendSelectAllItem = True Then
                    Me.Items.Insert(0, MyObj.GetDDLSelectAllItem())
                End If
            End If

            MyBase.OnDataBound(e)
        End Sub

        Public Property AppendSelectAllItem() As Boolean
            Get
                If ViewState("AppendSelectAllItem") Is Nothing Then
                    Return False
                End If

                Return ViewState("AppendSelectAllItem")
            End Get
            Set(ByVal value As Boolean)
                ViewState("AppendSelectAllItem") = value
            End Set
        End Property

        Public Property AppendPleaseChooseItem() As Boolean
            Get
                If ViewState("AppendPleaseChooseItem") Is Nothing Then
                    Return False
                End If

                Return ViewState("AppendPleaseChooseItem")
            End Get
            Set(ByVal value As Boolean)
                ViewState("AppendPleaseChooseItem") = value
            End Set
        End Property

        Public Property CancelAppendNullOnSingleItem() As Boolean
            Get
                If ViewState("CancelAppendNullOnSingleItem") Is Nothing Then
                    Return False
                End If

                Return ViewState("CancelAppendNullOnSingleItem")
            End Get
            Set(ByVal value As Boolean)
                ViewState("CancelAppendNullOnSingleItem") = value
            End Set
        End Property

        Protected Overrides Sub PerformSelect()
            Dim fv As FormView = TryCast(Me.NamingContainer, FormView)
            If fv IsNot Nothing And Me.DataSourceID = "" Then
                If fv.CurrentMode = FormViewMode.Insert Then
                    If Me.InsertDataSourceID <> "" Then
                        Me.DataSourceID = Me.InsertDataSourceID
                    End If
                ElseIf fv.CurrentMode = FormViewMode.Edit Then
                    If Me.EditDataSourceID <> "" Then
                        Me.DataSourceID = Me.EditDataSourceID
                    End If
                End If
            End If

            MyBase.PerformSelect()
        End Sub

        Public Property InsertDataSourceID() As String
            Get
                Return ViewState("InsertDataSourceID")
            End Get
            Set(ByVal value As String)
                ViewState("InsertDataSourceID") = value
            End Set
        End Property

        Public Property EditDataSourceID() As String
            Get
                Return ViewState("EditDataSourceID")
            End Get
            Set(ByVal value As String)
                ViewState("EditDataSourceID") = value
            End Set
        End Property

        ''' <summary>
        ''' Solve cascading dropdownlist bug
        ''' </summary>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
            Try
                MyBase.OnDataBinding(e)
            Catch ex As InvalidOperationException
                Me.GetData().Select(DataSourceSelectArguments.Empty, New DataSourceViewSelectCallback(AddressOf Me.DoSelect))
            Catch ex2 As Exception
                MyObj.WriteLog(ex2.Message)
            End Try

        End Sub

        Private Sub DoSelect(ByVal data As IEnumerable)
            Me.PerformDataBinding(data)
        End Sub


        'Protected Overrides Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean
        '    Dim values As String() = postCollection.GetValues(postDataKey)
        '    Dim selectedValue = values(0)

        '    Me.PreviousSelectedValue = selectedValue

        '    Return MyBase.LoadPostData(postDataKey, postCollection)
        'End Function

        'Public Property PreviousSelectedValue() As String
        '    Get
        '        Return ViewState("PreviousSelectedValue")
        '    End Get
        '    Set(ByVal value As String)
        '        ViewState("PreviousSelectedValue") = value
        '    End Set
        'End Property
    End Class
End Namespace
